Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support listOf, setOf with varargs #60

Merged
merged 24 commits into from
Jan 7, 2019
Merged

Conversation

passsy
Copy link
Owner

@passsy passsy commented Jan 3, 2019

The current API to create collections is rather verbose compared to kotlin. Since dart doesn't support varargs, it wasn't possible to copy Kotlins method signature.

// kotlin
listOf(1, 2, 3)

// kollection 0.3.0
listOf([1, 2, 3])

But it turned out to be a false impression of Darts capabilities. Although varargs aren't available the same API can be achieved using optional positional arguments!

// kollection 0.4.0
listOf(1, 2, 3)

To simplify things, null cannot be used as argument of listOf. Also, the number of arguments is limited to 10.
For lists with more elements and null values use the new listFrom(Iterable) method.

// kollection 0.4.0
listFrom([1, 2, 3])

Now every<type>Of(args*) function now has also a <type>From(Iterable) companion.

Additionally, as @rrousselGit pointed out, this library now offers a second way to create collections via factory constructors. Here's a complete list of all creation APIs:

void list() {
  emptyList<int>();
  listOf(1, 2, 3, 4, 5);
  listFrom([1, 2, 3, 4, 5]);

  mutableListOf(1, 2, 3, 4, 5);
  mutableListFrom([1, 2, 3, 4, 5]);

  KList<int>.empty();
  KList.of(1, 2, 3, 4, 5);
  KList.from([1, 2, 3, 4, 5]);

  KMutableList<int>.empty();
  KMutableList.of(1, 2, 3, 4, 5);
  KMutableList.from([1, 2, 3, 4, 5]);
}

void set() {
  emptySet<int>();
  setOf(1, 2, 3, 4, 5);
  setFrom([1, 2, 3, 4, 5]);

  KSet<int>.empty();
  KSet.of(1, 2, 3, 4, 5);
  KSet.from([1, 2, 3, 4, 5]);

  KHashSet<int>.empty();
  KHashSet.of(1, 2, 3, 4, 5);
  KHashSet.from([1, 2, 3, 4, 5]);

  KLinkedSet<int>.empty();
  KLinkedSet.of(1, 2, 3, 4, 5);
  KLinkedSet.from([1, 2, 3, 4, 5]);
}

void map() {
  emptyMap<int, String>();
  mapFrom({1: "a", 2: "b"});

  KMutableMap<int, String>.empty();
  KMutableMap.from({1: "a", 2: "b"});

  KHashMap<int, String>.empty();
  KHashMap.from({1: "a", 2: "b"});

  KLinkedMap<int, String>.empty();
  KLinkedMap.from({1: "a", 2: "b"});
}

This is a breaking change

This change is 100% breaking all existing implementations. But since this project is rather new (still 0.X), and not heavily used by many projects that's fine. It makes the API so much better!

  • Add tests
  • Update Readme

@passsy passsy changed the title Support listOf with varargs Support listOf, setOf with varargs Jan 3, 2019
@codecov
Copy link

codecov bot commented Jan 3, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@6833ac7). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #60   +/-   ##
=========================================
  Coverage          ?   99.46%           
=========================================
  Files             ?       40           
  Lines             ?     1693           
  Branches          ?        0           
=========================================
  Hits              ?     1684           
  Misses            ?        9           
  Partials          ?        0
Impacted Files Coverage Δ
lib/src/extension/map_extensions_mixin.dart 100% <100%> (ø)
lib/src/collection/map_linked.dart 100% <100%> (ø)
lib/src/k_set_hash.dart 100% <100%> (ø)
lib/src/util/arguments.dart 100% <100%> (ø)
lib/src/collection/list_empty.dart 100% <100%> (ø)
lib/src/extension/iterable_extension_mixin.dart 99.57% <100%> (ø)
lib/src/k_list_mutable.dart 100% <100%> (ø)
lib/src/collection/iterator.dart 100% <100%> (ø)
lib/src/k_set.dart 100% <100%> (ø)
lib/src/collection/set_hash_linked.dart 100% <100%> (ø)
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6833ac7...4b5ec0d. Read the comment docs.

} else if (arg0 != null) {
args = [arg0];
} else {
return emptyList();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const constructor?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KList can't offer a const constructor. See #62


This question gave me a sleepless night 😅. Thanks for asking

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to help. 👿
That's too bad. But in any case, since the list is immutable then always returning the same class instance even if not const, should be fine right?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't return KList<dynamic> for KList<String>

KList _empty = EmptyList();
KList<T> emptyList<T>() => _empty;

// error: type 'EmptyList<dynamic>' is not a subtype of type 'KList<String>'
emptyList<String>()

lib/src/collections.dart Outdated Show resolved Hide resolved
@passsy passsy merged commit 1b9c318 into master Jan 7, 2019
@passsy passsy deleted the feature/vararg_creations branch January 7, 2019 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants